// pages/login/login.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    phoneNumber: null,
    identifyingCode: null,
    passWord: null,
    repassWord: null,
    btnType: 1,
    defaultWaitTime: 60,
    waitTime: null,
    defaultWaitText: "验证",
    waitInterval: null
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },
  changeUserType: function (e) {
    var that = this;
    console.log(e.currentTarget.dataset.usertype);
    that.setData({
      userType: e.currentTarget.dataset.usertype
    });
  },
  changeInputData: function (e) {
    var that = this;
    var inputName = e.currentTarget.dataset.name;
    var inputVlaue = e.detail.value;
    if (inputName == "phoneNumber") {
      that.setData({
        phoneNumber: inputVlaue
      });
    }
    if (inputName == "identifyingCode") {
      that.setData({
        identifyingCode: inputVlaue
      });
    }
    if (inputName == "passWord") {
      that.setData({
        passWord: inputVlaue
      });
    }
    if (inputName == "repassWord") {
      that.setData({
        repassWord: inputVlaue
      });
    }
  },
  getIdentifyingCode: function(){
    var that = this;
    var reg = new RegExp('^[1][3,5,8][0-9]{9}$');
    var bid = wx.getStorageSync('bid');
    if (!reg.test(that.data.phoneNumber)) {
      wx.showToast({
        title: '手机号不正确',
        icon: 'none',
        mask: true,
      });
      return false;
    }
    
    that.setData({
      btnType: 0,
      waitTime: that.data.defaultWaitTime,
      waitInterval: setInterval(function(){
        if (that.data.waitTime <= 0) {
          clearInterval(that.data.waitInterval);
          that.setData({
            btnType: 1
          });
        }
        that.setData({
          waitTime: --that.data.waitTime
        });
      },1000)
    });
    // TODO 验证码获取



    app.request({
      url: api.default.checkcode,
      method: 'post',
      data: {
        bid:bid,
        phone: that.data.phoneNumber
      },
      success: function (e) {
        that.setData({
          Smscode: e.code
        })
      }
    })
  },
  sendRequest: function () {
    var that = this;
    if (that.validate()) {
      // TODO 发送请求
      app.request({
        url: api.default.reset,
        method: 'post',
        data: {
          phone: that.data.phoneNumber,
          password: that.data.passWord,
          type: that.data.userType,
          bid: wx.getStorageSync('bid')
        },
        success: function (e) {

          if (e.code == 1) {
            wx.removeStorageSync('cash');
            wx.removeStorageSync('phone');
            wx.showToast({
              title: e.msg,
              duration: 2000,
              success: function () {
               
                setTimeout(function () {
                  wx.navigateTo({
                    url: '../login/index',
                  })
                }, 2000)
              }
            })
          } else {
            wx.showToast({
              title: e.msg,
              icon: 'none',
              mask: true,
            })
          }
        }
      })
    }
  },
  validate: function () {
    var that = this;
    var reg = new RegExp('^[1][3,5,8][0-9]{9}$');

    if (!reg.test(that.data.phoneNumber)) {
      wx.showToast({
        title: '手机号不正确',
        icon: 'none',
        mask: true,
      });
      return false;
    }
    console.log(that.data.identifyingCode);
    if (!that.data.identifyingCode || that.data.identifyingCode.length <= 0) {
      wx.showToast({
        title: '验证码不得为空',
        icon: 'none',
        mask: true,
      });
      return false;
    }
    if (!that.data.passWord || that.data.passWord.length <= 0) {
      wx.showToast({
        title: '密码不得为空',
        icon: 'none',
        mask: true,
      });
      return false;
    }
    if (that.data.repassWord != that.data.passWord) {
      wx.showToast({
        title: '确认密码有误',
        icon: 'none',
        mask: true,
      });
      return false;
    }
    return true;
  }
})